package com.vietthanh85.android;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Vector;

import android.os.Build;
import android.os.Environment;

public class Common {
	public static String allWeeksFileName = "all.txt";
	public static String currentWeekFolder = "Android/data/com.dropbox.android/files/scratch/SocialWeb/English";
	public static String currentWeekFileName = "newWords.txt";	
	public static String currentFolder;
	public static String currentFileName;
	public static Map<String, String> typeMap = Collections
			.synchronizedMap(new LinkedHashMap<String, String>());
	public static Vector<Line> lineVector;
	public static Vector<Line> forgotLineVector;
	public static File sdCard;
	public static File newWordsFile;
	public static BufferedReader bufferReader;
	public static String[] dirArray;	
	public static boolean allSelected;
	public static boolean changed;
	public static boolean forgotListChanged;
	public static boolean allFileCreated;
	public static String weekSelected;
	public static boolean reversed = false;
	public static String client;
	public static SimpleDateFormat sdf;
	
	public static void init() {
		typeMap.put("(n)", "Noun");
		typeMap.put("(v)", "Verb");
		typeMap.put("(adj)", "Adjective");
		typeMap.put("(adv)", "Adverb");
		typeMap.put("(phr)", "Phrase");
		typeMap.put("(prep)", "Preposition");
		typeMap.put("<later>", "<later>");
		weekSelected = "CurrentWeek";
		currentFolder = currentWeekFolder;
		currentFileName = currentWeekFileName;
		allSelected = false;
		changed = false;
		forgotListChanged = false;
		allFileCreated = false;		
		lineVector = getDataFromFile(currentFolder, currentFileName);
		forgotLineVector = getDataFromFile(currentWeekFolder + "/Forgot", client + ".txt");
		client = Build.MODEL;
		sdf = new SimpleDateFormat("EEE, d MMM yyyy, HH:mm:ss");	
		File directory = new File(Environment.getExternalStorageDirectory(), currentWeekFolder + "/Outputs");		
		dirArray = directory.list();	
	}
	
	public static Vector<Line> getDataFromFile(String folder, String fileName) {
		Vector<Line> lineVector = new Vector<Line>();
		sdCard = new File(Environment.getExternalStorageDirectory(), folder);
		newWordsFile = new File(sdCard, fileName);
		int number = 0;
		try {
			bufferReader = new BufferedReader(new FileReader(newWordsFile));
			String line;
			String[] result;
			while ((line = bufferReader.readLine()) != null) {
				result = line.split("\t");
				Line lineIns = new Line();
				lineIns.id = ++number;
				lineIns.word = result[0];
				lineIns.type = result[1];
				lineIns.meaning = result[2];
				lineIns.client = result[3];
				lineIns.time = result[4];
				lineVector.add(lineIns);
			}
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return lineVector;
	}
	
	public static void removeIDFromVector(Vector<Line> lineVector, int id) {
		Iterator<Line> itr = lineVector.iterator();
		while (itr.hasNext()) {
			Line lineIns = itr.next();
			if (lineIns.id == id) {
				lineVector.remove(lineIns);
				break;
			}
		}
	}
	
	public static int word2Loc(Vector<Line> lineVector, String word) {
		Iterator<Line> itr = lineVector.iterator();
		int loc = 0;
		while (itr.hasNext()) {
			Line lineIns = itr.next();
			if (lineIns.word.equals(word)) {
				return loc;
			}
			loc++;
		}
		return -1;
	}
		
	public static void commitToFile(String folder, String fileName) {
		if (allSelected) {
			return;
		}
		sdCard = new File(Environment.getExternalStorageDirectory(), folder);
		if (!sdCard.exists()) {
			sdCard.mkdirs();
		}
		newWordsFile = new File(sdCard, fileName);
		FileWriter writer;		
		try {
			writer = new FileWriter(newWordsFile, false);			
			String line = "";
			Iterator<Line> itr = lineVector.iterator();
			while (itr.hasNext()) {
				Line lineIns = itr.next();
				line = lineIns.word + "\t";
				line += lineIns.type + "\t";				
				line += lineIns.meaning + "\t";
				line += lineIns.client + "\t";
				line += lineIns.time + "\n";
				writer.append(line);
			}			
			writer.flush();
			writer.close();			
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.gc();
	}
	
	public static void makeAllVector() {
		lineVector = new Vector<Line>();
		File eachFolder;
		File eachFile;
		String line;
		String[] result;
		try {
			int no = 0;
			for (int i = dirArray.length - 1; i >= 0; i--) {
				eachFolder = new File(
						Environment.getExternalStorageDirectory(), currentWeekFolder
								+ "/Outputs");
				eachFile = new File(eachFolder, dirArray[i]);								
				bufferReader = new BufferedReader(new FileReader(eachFile));
				while ((line = bufferReader.readLine()) != null) {				
						result = line.split("\t");
						Line lineIns = new Line();
						lineIns.id = ++no;
						lineIns.word = result[0];
						lineIns.type = result[1];
						lineIns.meaning = result[2];
						lineIns.client = result[3];
						lineIns.time = result[4];
						lineVector.add(lineIns);					
				}
			}
			eachFolder = new File(
					Environment.getExternalStorageDirectory(), currentWeekFolder);
			eachFile = new File(eachFolder, currentWeekFileName);
			bufferReader = new BufferedReader(new FileReader(eachFile));
			while ((line = bufferReader.readLine()) != null) {
				result = line.split("\t");
				Line lineIns = new Line();
				lineIns.id = ++no;
				lineIns.word = result[0];
				lineIns.type = result[1];
				lineIns.meaning = result[2];
				lineIns.client = result[3];
				lineIns.time = result[4];
				lineVector.add(lineIns);
			}
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}
}
